﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DelonixRegiaHotel.Classes;
using DelonixRegiaHotel.DBManager;
using System.Collections;

namespace DelonixRegiaHotel
{
    public partial class StaffEditInfo : Form
    {
        ArrayList staffInfo = StaffAndDutyDBManager.GetAllStaffInfo();
        public StaffEditInfo()
        {
            InitializeComponent();

            foreach (Staff s in staffInfo)
            {
                if (StaticSession.StaffID == s.StaffID)
                {
                    tbxStaffId.Text = s.StaffID;
                    tbxfn.Text = s.FirstName;
                    tbxln.Text = s.LastName;
                    tbxNric.Text = s.Nric;
                    tbxAddress.Text = s.HomeAddress;
                    tbxPostalCode.Text = Convert.ToString(s.PostalCode);
                    maskedTextBoxDob.Text = Convert.ToString(s.Dob.ToShortDateString());
                    tbxContactNo.Text = Convert.ToString(s.ContactNo);
                    tbxEmail.Text = s.Email;
                    tbxBankAccNo.Text = Convert.ToString(s.BankAccountNo);
                    tbxPassword.Text = s.Password;
                }

            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            //validate date
            DateTime dt;
            if (!DateTime.TryParse(maskedTextBoxDob.Text, out dt))
            {
                MessageBox.Show("Invalid Date");
                return;
            }

            if (tbxfn.Text.Equals("") || tbxln.Text.Equals("") || tbxNric.Text.Equals("") || tbxAddress.Text.Equals("") || tbxPostalCode.Text.Equals("") || maskedTextBoxDob.Text.Equals("") || tbxContactNo.Text.Equals("") || tbxEmail.Text.Equals("") || tbxBankAccNo.Text.Equals("") || tbxPassword.Text.Equals("") || maskedTextBoxDob.Text.Equals(""))
            {
                MessageBox.Show("Ensure that all fields are filled in");
                return;
            }

            if (tbxContactNo.TextLength != 8)
            {
                MessageBox.Show("Invalid contact number");
                return;
            }

            Staff updateStaff = new Staff();

            updateStaff.StaffID = tbxStaffId.Text;
            updateStaff.FirstName = tbxfn.Text;
            updateStaff.LastName = tbxln.Text;
            updateStaff.Nric = tbxNric.Text;
            updateStaff.HomeAddress = tbxAddress.Text;
            updateStaff.PostalCode = Convert.ToInt32(tbxPostalCode.Text);
            updateStaff.Dob = Convert.ToDateTime(maskedTextBoxDob.Text);
            updateStaff.ContactNo = Convert.ToInt32(tbxContactNo.Text);
            updateStaff.Email = tbxEmail.Text;
            updateStaff.BankAccountNo = Convert.ToInt32(tbxBankAccNo.Text);
            updateStaff.Password = tbxPassword.Text;

            if (StaffAndDutyDBManager.StaffEditOwnInfoByID(updateStaff) == 1)
                {
                    MessageBox.Show("Update successful");
                    this.Hide();
                }
            }
       

        //validate textboxes which only allow integers
        private void tbxBankAccNo_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                int intEntered = int.Parse(tbxBankAccNo.Text);
            }

            catch
            {
                e.Cancel = true;
                MessageBox.Show("Invalid entry");
            }
        }

        private void tbxPostalCode_Validating_1(object sender, CancelEventArgs e)
        {
            try
            {
                int intEntered = int.Parse(tbxPostalCode.Text);
            }

            catch
            {
                e.Cancel = true;
                MessageBox.Show("Invalid entry");
            }
        }

        private void tbxContactNo_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                int intEntered = int.Parse(tbxContactNo.Text);
            }

            catch
            {
                e.Cancel = true;
                MessageBox.Show("Invalid entry");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
